using System;
using System.Xml.Linq;
using Engine;
using Game;

namespace ApiToolkit
{
	public class Int32PropertyEditor : CanvasWidget
	{
		// Token: 0x06000021 RID: 33 RVA: 0x00002ACC File Offset: 0x00000CCC
		private void getValueLimit(out int start, out int end)
		{
			switch(Editor.TargetPropertyName)
			{
			    case "DisplayOrder":
				    start = 0;
				    end = int.MaxValue;
			    	break;
			    default:
			        start = int.MinValue;
	    		    end = int.MaxValue;
	    		    break;
			}
		}

		public Int32PropertyEditor(int content, string targetProperty, ContainerWidget parent)
		{
			try
			{
				this.Editor = new PropertyEditor_Common(content, targetProperty);
				XElement xelement = ContentManager.Get<XElement>("Widgets/Int32PropertyEditor", null);
				base.LoadContents(this, xelement);
				this.ValueSlider = this.Children.Find<SliderWidget>("ValueSlider", true);
				this.ValueTextBox = this.Children.Find<TextBoxWidget>("ValueTextBox", true);
				this.ApplyButton = this.Children.Find<ButtonWidget>("ApplyButton", true);
				this.value = (int)this.Editor.TargetPropertyValue;
				this.ValueTextBox.Text = this.value.ToString();
			}
			catch (Exception ex)
			{
				Log.Error(ex.ToString());
				throw;
			}
		}

		private int Clamp(int value, int min, int max)
		{
		    if(value < min) return min;
		    if(value > max) return max;
		    return value;
		}
	    
		public override void Update()
		{
			if (this.ValueSlider.SlidingCompleted)
			{
				int num = (int)this.ValueSlider.Value;
				int num2;
				int num3;
				this.getValueLimit(out num2, out num3);
				if (num + this.value < num2 || num + this.value > num3)
				{
					num = Clamp(this.value + num, num2, num3) - this.value;
				}
				this.value += num;
				this.ValueSlider.Value = 0f;
				this.ValueTextBox.Text = this.value.ToString();
			}
			else if (this.ValueSlider.IsSliding)
			{
				int num4 = (int)this.ValueSlider.Value;
				if (num4 != 0)
				{
					this.ValueTextBox.Text = (this.value + num4).ToString();
				}
			}
			else if (this.value.ToString() != this.ValueTextBox.Text)
			{
				int num5;
				if (this.ValueTextBox.Text.TryConvertToInt32(out num5))
				{
					this.value = num5;
				}
				else
				{
					this.ValueTextBox.Text = this.value.ToString();
				}
			}
			if (this.ApplyButton.IsClicked)
			{
				this.Editor.TargetPropertyValue = this.value;
			}
		}

		// Token: 0x04000021 RID: 33
		private ContainerWidget Parent;

		// Token: 0x04000022 RID: 34
		private SliderWidget ValueSlider;

		// Token: 0x04000023 RID: 35
		private TextBoxWidget ValueTextBox;

		// Token: 0x04000024 RID: 36
		private ButtonWidget ApplyButton;

		// Token: 0x04000025 RID: 37
		private PropertyEditor_Common Editor;

		// Token: 0x04000026 RID: 38
		private int value;
	}
}
